package com.ruoyi.buss.report.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.buss.utils.ListUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.report.mapper.ReportMarketPerformanceLsbMapper;
import com.ruoyi.buss.report.domain.ReportMarketPerformanceLsb;
import com.ruoyi.buss.report.service.IReportMarketPerformanceLsbService;

/**
 * 营销任务进度历史记录，每天保存4.1.1Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class ReportMarketPerformanceLsbServiceImpl implements IReportMarketPerformanceLsbService 
{
    @Autowired
    private ReportMarketPerformanceLsbMapper reportMarketPerformanceLsbMapper;

    /**
     * 查询营销任务进度历史记录，每天保存4.1.1
     * 
     * @param sjrq 营销任务进度历史记录，每天保存4.1.1ID
     * @return 营销任务进度历史记录，每天保存4.1.1
     */
    @Override
    public ReportMarketPerformanceLsb selectReportMarketPerformanceLsbById(String sjrq)
    {
        return reportMarketPerformanceLsbMapper.selectReportMarketPerformanceLsbById(sjrq);
    }

    /**
     * 查询营销任务进度历史记录，每天保存4.1.1列表
     * 
     * @param reportMarketPerformanceLsb 营销任务进度历史记录，每天保存4.1.1
     * @return 营销任务进度历史记录，每天保存4.1.1
     */
    @Override
    public List<ReportMarketPerformanceLsb> selectReportMarketPerformanceLsbList
                (ReportMarketPerformanceLsb reportMarketPerformanceLsb, LoginUser userInfo, int type)
    {
        if (!StringUtils.isEmpty(reportMarketPerformanceLsb.getSjrq())) {
            reportMarketPerformanceLsb.setSjrq(reportMarketPerformanceLsb.getSjrq().replace("-", ""));
        }

        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();

        //如果是admin用户不做处理
        if(userInfo.getRoles().contains("zhhz")){
            reportMarketPerformanceLsb.setOrgId(orgId);
        } else if(userInfo.getRoles().contains("common")){
            reportMarketPerformanceLsb.setOrgId(orgId);
            reportMarketPerformanceLsb.setUserId(userId);
        }

        List<ReportMarketPerformanceLsb> doList = new ArrayList<ReportMarketPerformanceLsb>();

        // 0表示进度表1表示趋势图
        if (type == 0) {
            doList = reportMarketPerformanceLsbMapper.selectReportMarketPerformanceLsbList(reportMarketPerformanceLsb);
            List<ReportMarketPerformanceLsb> list = reportMarketPerformanceLsbMapper.yxrwjdbListhz(reportMarketPerformanceLsb);
            if(!ListUtil.isNullOrEmpty(list)){
                ReportMarketPerformanceLsb vo = list.get(0);
                vo.setOrgName("汇总数据");
                doList.add(vo);
            }
        } /*else if (type == 1) {
            doList = reportMarketPerformanceLsbMapper.yxrwjdbListhz(reportMarketPerformanceLsb);
            //导出数据 (所有人)
        } */ else if (type == 4) {
            // 支行汇总
            doList = reportMarketPerformanceLsbMapper.yxrwjdbListZh(reportMarketPerformanceLsb);
            List<ReportMarketPerformanceLsb> list = reportMarketPerformanceLsbMapper.yxrwjdbListhz(reportMarketPerformanceLsb);
            if(!ListUtil.isNullOrEmpty(list)){
                ReportMarketPerformanceLsb vo = list.get(0);
                vo.setOrgName("汇总数据");
                doList.add(vo);
            }
        }/*else if (type == 6) {
            // 总行汇总
            doList = reportMarketPerformanceLsbMapper.yxrwjdbListZhhz(reportMarketPerformanceLsb);
        }*/

        return doList;
    }

    /**
     * 新增营销任务进度历史记录，每天保存4.1.1
     * 
     * @param reportMarketPerformanceLsb 营销任务进度历史记录，每天保存4.1.1
     * @return 结果
     */
    @Override
    public int insertReportMarketPerformanceLsb(ReportMarketPerformanceLsb reportMarketPerformanceLsb)
    {
        return reportMarketPerformanceLsbMapper.insertReportMarketPerformanceLsb(reportMarketPerformanceLsb);
    }

    /**
     * 修改营销任务进度历史记录，每天保存4.1.1
     * 
     * @param reportMarketPerformanceLsb 营销任务进度历史记录，每天保存4.1.1
     * @return 结果
     */
    @Override
    public int updateReportMarketPerformanceLsb(ReportMarketPerformanceLsb reportMarketPerformanceLsb)
    {
        return reportMarketPerformanceLsbMapper.updateReportMarketPerformanceLsb(reportMarketPerformanceLsb);
    }

    /**
     * 批量删除营销任务进度历史记录，每天保存4.1.1
     * 
     * @param sjrqs 需要删除的营销任务进度历史记录，每天保存4.1.1ID
     * @return 结果
     */
    @Override
    public int deleteReportMarketPerformanceLsbByIds(String[] sjrqs)
    {
        return reportMarketPerformanceLsbMapper.deleteReportMarketPerformanceLsbByIds(sjrqs);
    }

    /**
     * 删除营销任务进度历史记录，每天保存4.1.1信息
     * 
     * @param sjrq 营销任务进度历史记录，每天保存4.1.1ID
     * @return 结果
     */
    @Override
    public int deleteReportMarketPerformanceLsbById(String sjrq)
    {
        return reportMarketPerformanceLsbMapper.deleteReportMarketPerformanceLsbById(sjrq);
    }
}
